// pages/myCenter/employInto/employInto.js
import Common from '../../../common/common.js';
let common = new Common;
Page({
  data: {
    imgUrl: common.imgUrl
  },
  index() {
    wx.reLaunch({
      url: '/pages/login/index',
    })
  },
  bindGetUserInfo(e) {
    let headPicUrl = e.detail.userInfo.avatarUrl;
    let gender = e.detail.userInfo.gender;
    wx.reLaunch({
      url: '/pages/login/index?headPicUrl=' + headPicUrl + '&gender=' + gender,
    })
  },
  onLoad(options) {
    let yqUserId = options.userId; //邀请人的userId
    let yqOrgSeq = options.orgSeq;

    getApp().globalData.yaoQorgSeq = options.orgSeq;
    console.log('邀请人的门店编码', yqOrgSeq);
    console.log('邀请人的员工编码', yqUserId)

    this.setData({
      shopName: options.shopName
    })
    //1、检测登陆 根据userid判断是否是本人点击
    //2、是自己点的直接跳首页
    //3、是他人点击进入的 
    //4、判断 是否是 分享门店 的员工 
    //5、是则提示跳到对应门店首页；
    //6、不是则去注册登陆

    //检测登陆状态
    common.checkLogin().then(res => {
      if (res.data.code) {
        let userId = res.data.result.userId;
        if (userId == yqUserId) {
          //是本人
          console.log('是本人')
          wx.showToast({
            title: '即将返回首页！',
            icon: "none"
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }, 2200)
        } else {
          console.log('不是本人')
          //检测是否存在门店
          let orgList = res.data.result.orgList || [];
          let isHaveOrgSeq = false;
          isHaveOrgSeq = orgList.forEach((item, index) => {
            if (item.orgSeq == yqOrgSeq) {
              return true
            }
          })
          console.log(isHaveOrgSeq ? '是该门店的员工' : '不是该门店的员工');
          if (isHaveOrgSeq) {
            wx.showModal({
              title: '提示',
              content: '您已经是该门店的员工啦！',
              confirmText: "返回首页",
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
                }
              }
            })
          }
        }
      }
    }).catch(err => {
      console.log(err, '没有登陆过')
    })
  }
})